#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import logging
import pdb
import sys
from git import Repo
import git

'这个是冯秀伟专用的python工具库'
__author__='fengxw'

def active_branch( thisRepo ):
	try:
		return thisRepo.active_branch
	except BaseException:
		logging.error(" Error! skip this git ")
		return 'Failed!!!'
	else:
		return 'Success'

def show_all( thisRepo ):
	bnames =[]
	name=''
	try:
		current_bnh = active_branch( thisRepo )
		for r in thisRepo.refs:
			if(r == current_bnh):
				name = " * "+r.name
			else:
				name = r.name
			bnames.append(name)
	except BaseException:
		logging.error(" Error! skip this git ")
		return 'Failed!!!'
	else:
		return bnames

def is_dirty( thisRepo ):
	rst = ''
	try:
		if(True == thisRepo.is_dirty() ):
			rst = 'dirty'
		else:
			rst = 'not dirty'
	except BaseException:
		logging.error(" Error! skip this git ")
		return 'Failed!!!'
	else:
		return rst

def clear_with_checkout( thisRepo ):
	try:
		thisRepo.head.reset(index=True , working_tree=True)
	except BaseException:
		logging.error(" Error! skip this git ")
		return 'Failed!!!'
	else:
		return 'clear Success'

def pull( thisRepo ):
	rst = ' pull success '
	origin = git.remote.Remote(thisRepo ,'origin')
	origin.pull()
	return rst

def create_branch_from_current_branch( thisRepo , new_branch_name):
	try:
		thisRepo.create_head(new_branch_name)
	except BaseException:
		logging.error(" Error! skip this git ")
		return 'Failed!!!'
	else:
		return 'create new branch '+ new_branch_name + ' Success';

def delete_branch( thisRepo , be_del_branch_name):
	try:
		thisRepo.delete_head(be_del_branch_name)
	except BaseException:
		logging.error(" Error! skip this git ")
		return 'Failed!!!'
	else:
		return 'delete branch:'+be_del_branch_name+' Success'

def switch_branch_soft( thisRepo , dst_branch_name):
	try:
		dst_branch = thisRepo.create_head(dst_branch_name ,'HEAD')
		thisRepo.head.reference = dst_branch
		dst_branch.checkout()
	except BaseException:
		logging.error(" Error! skip this git ")
		return 'Failed!!!'
	else:
		return 'switch branch:'+dst_branch_name+' soft Success'

def switch_branch_force( thisRepo , dst_branch_name):
	try:
		dst_branch = thisRepo.create_head(dst_branch_name ,'HEAD')
		thisRepo.head.reference = dst_branch
		thisRepo.head.reset(index=True, working_tree=True)
	except BaseException:
		logging.error(" Error! skip this git ")
		return 'Failed!!!'
	else:
		return 'switch branch:'+dst_branch_name+' force Success'

def checkout_only( thisRepo , dst_branch_name ):
	git_cmd = thisRepo.git
	try:
		git_cmd.checkout(dst_branch_name)
	except BaseException:
		logging.error(" Error! skip this git ")
		return 'Failed!!!'
	else:
		return 'checkout :'+dst_branch_name+' only , Success'

def fetch_only( thisRepo ):
	try:
		thisRepo.remotes.origin.fetch()
	except BaseException:
		logging.error(" Error! skip this git ")
		return 'Failed!!!'
	else:
		return 'from origin fetch only ,Success'
